<template>
  <view class="page">
    <view class="top-bg" v-if="news && isLoadingFinish">
      <image
        :lazy-load="true"
        class="bg"
        :src="imgBaseUrlV0 + 'news-detail-top-bg.png'"
        mode="widthFix"
      ></image>
      <view class="bg-title">
        <text class="chn">动态详情</text>
        <text class="en">Dynamic Detail</text>
      </view>
    </view>
    <view class="news-main" v-if="news && isLoadingFinish">
      <text class="title">{{ news.title }}</text>
      <text class="time">{{ news.creationTimeFormat }}</text>
      <view class="desrciption-view" v-if="news.brief">
        <text class="desrciption">{{ news.brief }}</text>
      </view>
      <view class="content">
        <u-parse :html="news.content"></u-parse>
      </view>
    </view>

    <popup-loading ref="popup"> </popup-loading>
  </view>
</template>

<script>
import { getNewsInfo, readNews } from "@/common/api/news.js";
import parseHtml from "@/common/html-parser.js";
import popupLoading from "@/components/public/loading/popup";
import { mapState } from "vuex";
import { delayFunc } from "@/common/util";
import { imgBaseUrl, imgBaseUrlV0 } from "@/common/config/index";
export default {
  components: {
    popupLoading,
  },
  computed: {
    ...mapState("User", ["userInfo"]),
  },
  data() {
    return {
      newsId: undefined,
      news: null,
      isLoadingFinish: false,
      imgBaseUrlV0,
      contentParse: [],
    };
  },
  async onLoad(e) {
    this.historyList = this.$storage.get("readNews") || [];
    if (e.title) {
      uni.setNavigationBarTitle({
        title: e.title,
      });
    }
    this.$nextTick(() => {
      this.open();
      if (e.id !== undefined) {
        this.newsId = e.id;
      }
      this.initData();
    });
  },
  methods: {
    parseHtml,
    initData() {
      getNewsInfo(this.newsId).then(async (res) => {
        let data = res.data;
        this.news = data;
        this.contentParse = this.parseHtml(data.content || "");
        let index = this.historyList.findIndex((i) => i.id == this.newsId);
        let item = this.historyList[index] || { id: this.newsId, time: 0 };
        let lastTime = item.time || 0;
        if (!lastTime || +new Date() - lastTime >= 1000 * 7200) {
          if (this.historyList.length > 100) {
            this.historyList.length = 100;
          }
          item.time = +new Date();
          if (index > -1) {
            this.historyList.splice(index, 1, item);
          } else {
            this.historyList.push(item);
          }
          this.$storage.set("readNews", this.historyList);
          readNews(this.newsId);
        }
        this.close();
      });
    },

    open() {
      this._time = +new Date();
      this.$refs.popup && this.$refs.popup.open();
    },
    async close() {
      let now = +new Date();
      let time = this._time;
      let t = now - time;
      if (t <= 1000 * 0.5) {
        await delayFunc(500 - t);
      }
      this.isLoadingFinish = true;
      this.$refs.popup && this.$refs.popup.close();
    },
  },
};
</script>

<style lang="scss" scoped>
page,
.page {
  min-height: calc(100vh - var(--window-top));
  background-color: #fff;
}

.top-bg {
  position: relative;

  .bg {
    width: 100%;
  }
  .bg-title {
    position: absolute;
    height: 100rpx;
    top: 50%;
    width: 300rpx;
    left: 50%;
    margin-left: -150rpx;
    text-align: center;
    margin-top: -55rpx;

    .chn {
      display: block;
      font-size: 32rpx;
      font-weight: 700;
      line-height: 50rpx;
      color: #fff;
    }
    .en {
      display: block;
      font-size: 22rpx;
      line-height: 30rpx;
      color: #ddd;
    }
  }
}

.news-main {
  padding-top: 40rpx;

  .title {
    display: block;
    text-align: center;
    font-size: 34rpx;
    line-height: 60rpx;
    color: #000;
    font-weight: 700;
    padding: 0 30rpx;
  }
  .time {
    display: block;
    text-align: center;
    font-size: 22rpx;
    color: #838383;
  }
  .desrciption-view {
    margin: 20rpx 30rpx;
    padding-right: 20rpx;
    position: relative;
    z-index: 2;

    .desrciption {
      position: relative;
      z-index: 3;
      display: block;
      background-image: linear-gradient(
        to right bottom,
        #79c0ff,
        #3575df
      ) !important;
      // margin-right: 20rpx;
      padding: 40rpx;
      color: #fff;
      font-size: 26rpx;
      line-height: 50rpx;
    }
    &::before {
      content: "";
      position: absolute;
      z-index: 1;
      top: 20rpx;
      right: 0;
      width: calc(100% - 20rpx);
      height: 100%;
      background-color: #d9e5f9;
    }
  }
  .content {
    display: block;
    padding: 30rpx;
    padding-top: 40rpx;
    line-height: 50rpx;
  }
}
</style>